package cn.miju.ser.common.task;

import cn.miju.common.util.CronUtil;
import cn.miju.ser.common.rocketmq.DelayLevel;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * <pre>
 * 定时任务配置类
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-03-11 15:58
 */
@Configuration
@EnableScheduling
public class QuartzAutoConfig {
    @Value("${miju.mq-delay-level}")
    private DelayLevel delayLevel;
    /**
     * 创建待支付订单超时处理定时任务
     */
    @Bean("waitingOrderDetail")
    public JobDetail waitingOrderDetail() {
        return JobBuilder.newJob(WaitingPayOrderQuartzJob.class).
                withDescription("待支付订单超时处理定时任务（消息队列未处理/未成功处理）").
                usingJobData("query_order_time_second", delayLevel.getDelayTime() + (3 * 60)).
                storeDurably().
                build();
    }

    /**
     * 创建待支付订单超时处理定时任务触发器
     */
    @Bean("waitingOrderTrigger")
    public Trigger waitingOrderTrigger() {
        return TriggerBuilder.newTrigger().
                forJob(waitingOrderDetail()).
                withDescription("待支付订单超时处理定时任务（消息队列未处理/未成功处理）").
                withSchedule(CronScheduleBuilder.cronSchedule(CronUtil.createCronExpression(delayLevel.getDelayTime()))).
                build();
    }
}
